﻿Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary


Public Class SMDHelper

    Dim met As MetGeneral
    Public Sub New(ByVal data As MetGeneral)
        met = data
    End Sub
    Public Sub GeneralMetExprotsAll(ByVal FileName As String)
        Dim m_filename As String
        m_filename = FileName
        Dim m_LocationNum As String = "" '站点号 值00000到-99999 空值处理 无
        Dim m_ASKY As String = "" 'ASOS标志 值blankor A  A表示数据来源ASOS站点  空值处理 空格


        Dim m_DateTime As DateTime = Now '年        '月        '日        '时   空值处理 无
        Dim m_Year As String = ""
        Dim m_Month As String = ""
        Dim m_Day As String = ""
        Dim m_Hour As String = ""
        Dim Grad As String = "9999" 'GRAD  值0000 - 1415    空值处理9999
        Dim Drad As String = "9999" 'DRAD  值0000 - 1415    空值处理9999
        Dim m_TotalCloud As String = "99" '总云量  值00 - 10      空值处理99
        Dim m_LowCloud As String = "99" '低云量  值00 - 10      空值处理99
        Dim m_DryTemp As String = "999.9" '干球温度  值-70.0 - 60.0   空值处理999.9
        Dim m_InsertSign As String = " " '内插标志   值 空格或者I  空值处理 空格
        Dim m_DewTemp As String = "999.9" '漏点温度 值-70.0 60.0  空值处理 999.9
        Dim m_Humidity As String = "9999" '相对湿度   值000 - 100 百分数  空值处理 9999
        Dim m_SeaPressure As String = "9999" '站点气压  值0700 -1100  百帕 空值处理 9999
        '内插标志
        Dim m_Vane As String = "999" '风向  值000 - 360 度 空值处理  999
        Dim m_WindSpeed As String = "99.9" '风速  M/s   空值处理 99.9
        Dim m_Visibility As String = "999.9" '能见度 0 - 777.7 千米  空值处理 999.9
        Dim m_CloudHeight As String = "99999" '云底高度  00000 - 88888  米 空值处理  99999
        Dim m_PWTH As String = "00009999" '现时天气  天气条件  00009999
        'ASOS CLOUD LAYER 1  空值处理 99999
        'ASOS CLOUD LAYER 2   空值处理 99999
        'ASOS CLOUD LAYER 3   空值处理 99999
        Dim AsosCloudLayer1 As String = "99999"
        Dim AsosCloudLayer2 As String = "99999"
        Dim AsosCloudLayer3 As String = "99999"
        Dim m_Rainfall As String = "0" '小时降雨量   值  0 - 999  空值处理  0
        Dim m_RainType As String = " " '降雨标志  值 空格 A、 D、 M   空值处理  空格
        Dim m_SnowDeepth As String = "0" '雪深  值 000 - 100  英寸 空值处理 999
        Dim Str As String
        Dim swr As StreamWriter 'Dim ws As Double
        swr = New StreamWriter(FileName)
        Dim cc As Integer = 0

        'Dim m_ArrayMetData(-1) As Met.StructureGeneralMet() '常规气象数组，用于储存常规气象数据

        For i As Integer = -1 To Me.met.ArrayMetData.Length - 1

            If (i = -1) Then
                Str = "  1     " + "2        " + "3    " + "4    " + "5 " + "6    " + "7      " + "8    " + "9  " + "10    " + "11  " + "12     " + "13    " + "14      " + "15     " + "16    " + "17    " + "18     " + "19   " + "20" '& vbCrLf
                swr.WriteLine(Str)
            Else

                cc += 1
                If cc = 3 Then
                    cc = 0
                End If

                'ReDim Preserve m_ArrayMetData(i)
                'm_ArrayMetData(i) = New StructureGeneralMet

                m_LocationNum = Me.met.Location.SiteId                                '站台编号
                m_ASKY = " "

                m_Year = Mid(Me.met.ArrayMetData(i).m_DateTime.Year.ToString, 1, 4)            '年

                If Me.met.ArrayMetData(i).m_DateTime.Month.ToString.Length = 1 Then
                    m_Month = " " & Me.met.ArrayMetData(i).m_DateTime.Month.ToString         '月
                Else
                    m_Month = Me.met.ArrayMetData(i).m_DateTime.Month.ToString
                End If
                If Me.met.ArrayMetData(i).m_DateTime.Day.ToString.Length = 1 Then            '日
                    m_Day = " " & Me.met.ArrayMetData(i).m_DateTime.Day.ToString
                Else
                    m_Day = Me.met.ArrayMetData(i).m_DateTime.Day.ToString
                End If

                If Me.met.ArrayMetData(i).m_DateTime.Hour.ToString.Length = 1 Then            '时
                    If Me.met.ArrayMetData(i).m_DateTime.Hour = 9 Then
                        m_Hour = ((Me.met.ArrayMetData(i).m_DateTime.Hour) + 1).ToString
                    Else
                        m_Hour = " " & ((Me.met.ArrayMetData(i).m_DateTime.Hour) + 1).ToString
                    End If
                Else
                    m_Hour = ((Me.met.ArrayMetData(i).m_DateTime.Hour) + 1).ToString
                End If

                Grad = "9999"
                Drad = "9999"
                'If Me.ArrayMetData(i).m_TotalCloud Is Null Then
                If Trim(Me.met.ArrayMetData(i).m_TotalCloud.ToString) Is Nothing Then                '总云量
                    m_TotalCloud = "99"
                Else
                    m_TotalCloud = Convert.ToDouble(FormatNumber(Me.met.ArrayMetData(i).m_TotalCloud, 0)).ToString
                    If m_TotalCloud.Length < 2 Then
                        m_TotalCloud = m_TotalCloud.PadLeft(2, "0")
                    Else
                        m_TotalCloud = Mid(m_TotalCloud, 1, 2)
                    End If

                End If

                If Trim(Me.met.ArrayMetData(i).m_LowCloud.ToString) Is Nothing Then                  ' 低云量
                    m_LowCloud = "99"
                Else
                    m_LowCloud = Convert.ToDouble(FormatNumber(Me.met.ArrayMetData(i).m_LowCloud, 0)).ToString
                    If m_LowCloud.Length < 2 Then
                        m_LowCloud = m_LowCloud.PadLeft(2, "0")
                    Else
                        m_LowCloud = Mid(m_LowCloud, 1, 2)
                    End If
                End If

                If Trim(Me.met.ArrayMetData(i).m_DryTemp.ToString) Is Nothing Then                '干球温度
                    m_DryTemp = "999.9"
                Else
                    If Me.met.ArrayMetData(i).m_DryTemp = 999 Then
                        m_DryTemp = "999.9"
                    Else
                        If Me.met.ArrayMetData(i).m_DryTemp < 0 Then
                            ' m_DryTemp = (Me.ArrayMetData(i).m_DryTemp / 10 * 70 / 30).ToString
                            m_DryTemp = Format(Me.met.ArrayMetData(i).m_DryTemp / 10, "0.0")
                            If m_DryTemp.Length < 5 Then
                                m_DryTemp = m_DryTemp.PadLeft(5, " ")
                            ElseIf m_DryTemp.Length = 5 Then
                                m_DryTemp = Mid(m_DryTemp, 1, 5)
                            ElseIf Mid(m_DryTemp, 5, 1) = "." Then
                                m_DryTemp = " " + Mid(m_DryTemp, 1, 4)
                            Else
                                m_DryTemp = Mid(m_DryTemp, 1, 5)
                            End If
                        Else
                            'm_DryTemp = (Me.ArrayMetData(i).m_DryTemp / 10 * 60 / 35).ToString
                            m_DryTemp = Format(Me.met.ArrayMetData(i).m_DryTemp / 10, "0.0")
                            If m_DryTemp.Length < 5 Then
                                m_DryTemp = m_DryTemp.PadLeft(5, " ")
                            ElseIf m_DryTemp.Length = 5 Then
                                m_DryTemp = Mid(m_DryTemp, 1, 5)
                            ElseIf Mid(m_DryTemp, 5, 1) = "." Then
                                m_DryTemp = " " + Mid(m_DryTemp, 1, 4)
                            Else
                                m_DryTemp = Mid(m_DryTemp, 1, 5)
                            End If
                        End If


                    End If

                End If

                m_InsertSign = " "                                               '内插标志

                If Trim((Me.met.ArrayMetData(i).m_DewTemp) / 10).ToString Is Nothing Then                  ' 漏点温度
                    m_DewTemp = "999.9"
                Else
                    If ((Me.met.ArrayMetData(i).m_DewTemp)) = 999 Then
                        m_DewTemp = "999.9"
                    Else
                        If Me.met.ArrayMetData(i).m_DewTemp < 0 Then
                            'm_DewTemp = ((Me.ArrayMetData(i).m_DewTemp) / 10 * 70 / 65).ToString
                            m_DewTemp = Format(Me.met.ArrayMetData(i).m_DewTemp / 10, "0.0") '((Me.met.ArrayMetData(i).m_DewTemp) / 10).ToString 
                            If m_DewTemp.Length < 5 Then
                                m_DewTemp = m_DewTemp.PadLeft(5, " ")
                            ElseIf m_DewTemp.Length = 5 Then
                                m_DewTemp = Mid(m_DewTemp, 1, 5)
                            ElseIf Mid(m_DewTemp, 5, 1) = "." Then
                                m_DewTemp = " " + Mid(m_DewTemp, 1, 4)
                            Else
                                m_DewTemp = Mid(m_DewTemp, 1, 5)
                            End If
                        Else
                            'm_DewTemp = ((Me.ArrayMetData(i).m_DewTemp) / 10 * 60 / 35).ToString
                            m_DewTemp = Format(Me.met.ArrayMetData(i).m_DewTemp / 10, "0.0") ' ((Me.met.ArrayMetData(i).m_DewTemp) / 10).ToString
                            If m_DewTemp.Length < 5 Then
                                m_DewTemp = m_DewTemp.PadLeft(5, " ")
                            ElseIf m_DewTemp.Length = 5 Then
                                m_DewTemp = Mid(m_DewTemp, 1, 5)
                            ElseIf Mid(m_DewTemp, 5, 1) = "." Then
                                m_DewTemp = " " + Mid(m_DewTemp, 1, 4)
                            Else
                                m_DewTemp = Mid(m_DewTemp, 1, 5)
                            End If
                        End If


                    End If
                End If

                If Trim(((Me.met.ArrayMetData(i).m_Humidity)).ToString) Is Nothing Then                  ' 相对湿度
                    m_Humidity = "999"
                Else
                    If ((Me.met.ArrayMetData(i).m_Humidity)) = 999 Then
                        m_Humidity = "999"
                    Else
                        m_Humidity = Convert.ToDouble(FormatNumber(((Me.met.ArrayMetData(i).m_Humidity)), 0)).ToString
                        If m_Humidity.Length < 3 Then
                            m_Humidity = m_Humidity.PadLeft(3, "0")
                        Else
                            m_Humidity = Mid(m_Humidity, 1, 3)
                        End If
                    End If
                End If

                If Trim(Me.met.ArrayMetData(i).m_NowPressure.ToString) Is Nothing Then          '站点气压
                    m_SeaPressure = "9999"
                Else
                    If Me.met.ArrayMetData(i).m_NowPressure = 99999 Then
                        m_SeaPressure = "9999"
                    Else
                        'm_SeaPressure = ((Me.ArrayMetData(i).m_NowPressure) / 10 * 1800 / 19.999).ToString
                        'm_SeaPressure = (((Me.ArrayMetData(i).m_NowPressure) / 10 - 900) / 199.9 * 400 + 700).ToString
                        m_SeaPressure = Convert.ToDouble(FormatNumber(((Me.met.ArrayMetData(i).m_NowPressure) / 10), 0)).ToString
                        If m_SeaPressure.Length < 4 Then
                            m_SeaPressure = m_SeaPressure.PadLeft(4, " ")
                        Else
                            m_SeaPressure = Mid(m_SeaPressure, 1, 4)
                        End If
                    End If
                End If

                If Trim(((Me.met.ArrayMetData(i).m_Vane) / 10).ToString) Is Nothing Then
                    m_Vane = "999"
                Else
                    If ((Me.met.ArrayMetData(i).m_Vane)) = 99 Then
                        m_Vane = "999"
                    End If
                    If ((Me.met.ArrayMetData(i).m_Vane)) = 80 Then
                        m_Vane = "000"
                    End If
                    If ((Me.met.ArrayMetData(i).m_Vane)) <> 99 And ((Me.met.ArrayMetData(i).m_Vane)) <> 80 Then
                        m_Vane = Convert.ToDouble(FormatNumber(((Me.met.ArrayMetData(i).m_Vane) * 10), 0)).ToString()           '风向
                        ' m_Vane = ((Me.ArrayMetData(i).m_Vane) /10).ToString()            '风向
                    End If
                End If

                If m_Vane.Length < 3 Then
                    m_Vane = m_Vane.PadLeft(3, " ")
                Else
                    m_Vane = Mid(m_Vane, 1, 3)
                End If


                If Trim(Me.met.ArrayMetData(i).m_WindSpeed.ToString) Is Nothing Then                     '风速
                    m_WindSpeed = "99.9"
                Else
                    If Me.met.ArrayMetData(i).m_WindSpeed = -9999 Then
                        m_WindSpeed = "99.9"
                    Else
                        m_WindSpeed = Format(Me.met.ArrayMetData(i).m_WindSpeed / 10, "0.0")  '((Me.met.ArrayMetData(i).m_WindSpeed) / 10).ToString
                    End If
                End If





                If m_WindSpeed.Length = 1 Then

                    m_WindSpeed = m_WindSpeed.PadLeft(2, " ") + ".0"

                ElseIf m_WindSpeed.Length < 4 Then

                    m_WindSpeed = m_WindSpeed.PadLeft(4, " ")
                ElseIf m_WindSpeed.Length = 4 Then
                    m_WindSpeed = Mid(m_WindSpeed, 1, 4)
                ElseIf Mid(m_WindSpeed, 4, 1) = "." Then
                    m_WindSpeed = " " + Mid(m_WindSpeed, 1, 3)
                Else
                    m_WindSpeed = Mid(m_WindSpeed, 1, 4)
                End If

                If Trim(((Me.met.ArrayMetData(i).m_Visibility) / 10).ToString) Is Nothing Then                  ' 能见度
                    m_Visibility = "999.9"
                Else
                    ' If ((Me.ArrayMetData(i).m_Visibility) / 10) = 99999 Then
                    If ((Me.met.ArrayMetData(i).m_Visibility)) = 99999 Then
                        m_Visibility = "999.9"
                    Else
                        'm_Visibility = ((Me.ArrayMetData(i).m_Visibility) / 10 * 777.7 / 164).ToString
                        m_Visibility = ((Me.met.ArrayMetData(i).m_Visibility) / 10).ToString
                        If m_Visibility.Length < 5 Then
                            m_Visibility = m_Visibility.PadLeft(5, " ")
                        ElseIf m_Visibility.Length = 5 Then
                            m_Visibility = Mid(m_Visibility, 1, 5)
                        ElseIf Mid(m_Visibility, 5, 1) = "." Then
                            m_Visibility = " " + Mid(m_Visibility, 1, 4)
                        Else
                            m_Visibility = Mid(m_Visibility, 1, 5)
                        End If
                    End If
                End If


                If Me.met.ArrayMetData(i).m_LowCloud = 0 Then
                    m_CloudHeight = "77777"
                Else
                    If Trim(((Me.met.ArrayMetData(i).m_CloudHeight)).ToString) Is Nothing Then
                        'If Trim(((m_CloudHeight2(i)) * 100).ToString) Is Nothing Then                     ' 云底高度  新文件 米  国际标准 表格 km
                        'If Trim(((Me.ArrayMetData(i).m_CloudHeight2) * 100).ToString) Is Nothing Then                     ' 云底高度  新文件 米  国际标准 表格 km
                        m_CloudHeight = "77777"
                    Else
                        'If (m_CloudHeight2(i)) = 999 Then
                        If (Me.met.ArrayMetData(i).m_CloudHeight) = 999 Then
                            m_CloudHeight = "77777"
                        Else

                            m_CloudHeight = Convert.ToDouble(FormatNumber(((Me.met.ArrayMetData(i).m_CloudHeight) * 100), 0)).ToString
                            ' m_CloudHeight = ((Me.ArrayMetData(i).m_CloudHeight2) * 100 * 88.888 / 30).ToString
                            'm_CloudHeight = ((Me.ArrayMetData(i).m_CloudHeight) * 10).ToString
                            If m_CloudHeight.Length < 5 Then
                                m_CloudHeight = m_CloudHeight.PadLeft(5, " ")
                            Else
                                m_CloudHeight = Mid(m_CloudHeight, 1, 5)
                            End If
                        End If
                    End If
                End If




                'If (((Me.ArrayMetData(i).m_PWVC)).ToString) Is Nothing Then                  ' 现时天气
                m_PWTH = "00009999"
                '    Else
                '    m_PWTH = ((Me.ArrayMetData(i).m_PWVC)).ToString
                '    If m_PWTH.Length < 8 Then
                '        m_PWTH = m_PWTH.PadLeft(8, "0")
                '    Else
                '        m_PWTH = Mid(m_PWTH, 1, 8)
                '    End If
                'End If

                If (((Me.met.ArrayMetData(i).m_Rainfall)).ToString) Is Nothing Then             ' 小时降雨量
                    m_Rainfall = "  0"
                Else
                    If ((Me.met.ArrayMetData(i).m_Rainfall)) = -9 Then
                        m_Rainfall = "  0"
                    Else
                        ' m_Rainfall = ((Me.ArrayMetData(i).m_Rainfall) / 1000 * 999 / 25.4).ToString
                        m_Rainfall = Convert.ToDouble(FormatNumber(((Me.met.ArrayMetData(i).m_Rainfall) / 1000 / 25.4 / 100), 0)).ToString '1英寸=25.4毫米
                        If m_Rainfall.Length < 3 Then
                            m_Rainfall = m_Rainfall.PadLeft(3, " ")
                        Else
                            m_Rainfall = Mid(m_Rainfall, 1, 3)
                        End If
                    End If


                End If

                'm_Rainfall = " "                                                 '降雨标志
                m_SnowDeepth = "  0"                                              '雪深



                Str = Me.met.Location.SiteId + m_ASKY + m_Year.ToString & m_Month.ToString & m_Day.ToString & m_Hour.ToString
                Str += " " + Grad + " " + Drad + " " + m_TotalCloud + m_LowCloud + " " + m_DryTemp + "  " + m_DewTemp + " " + m_Humidity + " " + m_SeaPressure + "  " + m_Vane + " " + m_WindSpeed + "  " + m_Visibility + " " + m_CloudHeight + " " + m_PWTH + " " + "99999" + " " + "99999" + " " + "99999" + "  " + m_Rainfall + "  " + m_SnowDeepth '& vbCrLf
                swr.WriteLine(Str)
            End If
        Next


        'swr.Write(vbCrLf)
        swr.Close()
        swr.Dispose()


    End Sub
    '小李
    '2010-10-27  降水CPrePrecipitation类中OnBeforeRun事件中用于将内存中的数据导出到文件

    Public Sub GeneralMetExprotsTD3240(ByVal m_SaveFileName As String)
        Dim m_LocationNum As String = ""
        Dim m_DateTime As DateTime = Now '年        '月        '日        '时   空值处理 无
        Dim m_Year As String = ""
        Dim m_Month As String = ""
        Dim m_Day As String = ""
        Dim m_Hour As String = ""
        Dim m_Rainfall As String = ""  '空值为99999

        Dim Str As String
        Dim swr As StreamWriter
        swr = New StreamWriter(m_SaveFileName)
        Dim cc As Integer = 0

        For i As Integer = 0 To Me.met.ArrayMetData.Length - 1



            'ReDim Preserve m_ArrayMetData(i)
            'm_ArrayMetData(i) = New StructureGeneralMet

            'm_LocationNum = Me.Location.SiteId                                '站台编号

            If Me.met.Location.SiteId.Length = 5 Then
                m_LocationNum = Me.met.Location.SiteId + "000"                               '站台编号
            ElseIf Me.met.Location.SiteId.Length < 5 Then
                m_LocationNum = Me.met.Location.SiteId.PadRight(5, "0") + "000"                               '站台编号
            Else
                m_LocationNum = Me.met.Location.SiteId.Substring(0, 5) + "000"
            End If


            m_Year = Mid(Me.met.ArrayMetData(i).m_DateTime.Year.ToString, 1, 4)            '年

            If Me.met.ArrayMetData(i).m_DateTime.Month.ToString.Length = 1 Then
                m_Month = "0" & Me.met.ArrayMetData(i).m_DateTime.Month.ToString         '月
            Else
                m_Month = Me.met.ArrayMetData(i).m_DateTime.Month.ToString
            End If
            If Me.met.ArrayMetData(i).m_DateTime.Day.ToString.Length = 1 Then            '日
                m_Day = "000" & Me.met.ArrayMetData(i).m_DateTime.Day.ToString
            Else
                m_Day = "00" & Me.met.ArrayMetData(i).m_DateTime.Day.ToString
            End If

            If Me.met.ArrayMetData(i).m_DateTime.Hour.ToString.Length = 1 Then            '时
                If Me.met.ArrayMetData(i).m_DateTime.Hour = 9 Then
                    m_Hour = ((Me.met.ArrayMetData(i).m_DateTime.Hour) + 1).ToString + "00"
                Else
                    m_Hour = "0" & ((Me.met.ArrayMetData(i).m_DateTime.Hour) + 1).ToString + "00"
                End If
            Else
                m_Hour = ((Me.met.ArrayMetData(i).m_DateTime.Hour) + 1).ToString + "00"
            End If



            If (((Me.met.ArrayMetData(i).m_Rainfall)).ToString) Is Nothing Then             ' 小时降雨量
                m_Rainfall = "00000"
            Else
                If ((Me.met.ArrayMetData(i).m_Rainfall)) = -9 Then
                    m_Rainfall = "00000"
                Else
                    ' m_Rainfall = ((Me.ArrayMetData(i).m_Rainfall) / 1000 * 999 / 25.4).ToString
                    m_Rainfall = Convert.ToDouble(FormatNumber(((Me.met.ArrayMetData(i).m_Rainfall) * 100 / 1000 / 25.4), 0)).ToString '1英寸=25.4毫米
                    If m_Rainfall.Length < 5 Then
                        m_Rainfall = m_Rainfall.PadLeft(5, "0")
                    Else
                        m_Rainfall = Mid(m_Rainfall, 1, 5)
                    End If
                End If
            End If




            Str = "HPD" + m_LocationNum + "HPCPHI" + m_Year.ToString & m_Month.ToString & m_Day.ToString + "001" & m_Hour.ToString + "0" + m_Rainfall
            swr.WriteLine(Str)
            ' ''HPD54715000HPCPHI20060100010100000000
        Next

        'swr.Write(vbCrLf)
        swr.Close()
        swr.Dispose()

    End Sub
End Class
